﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using NUS.ISS.ARS.Service.Entity;
using NUS.ISS.ARS.BusinessFacade.Interface;
using NUS.ISS.ARS.BusinessFacade;

namespace NUS.ISS.ARS.Service
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class FlightSchedule : IFlightSchedule
    {
        private IAdminBF _adminBF;
        private IBookingBF _bookingBF;
        public FlightSchedule()
        {
            _adminBF = new AdminBF();
            _bookingBF = new BookingBF();
        }

        public ICollection<FlightScheduleViewData> GetAllFlightSchedules()
        {
              ICollection<FlightScheduleViewData> flightscheduleviewdatas = new List<FlightScheduleViewData>();
              FlightScheduleViewData flightscheduleviewdata=new FlightScheduleViewData();
            try
            {
                foreach (var item in _adminBF.IEnumGetAllFlightSchedules())
                {
                        flightscheduleviewdata.FlightNo=item.FlightNumber;
                        flightscheduleviewdata.DepartureTime=item.DepartureTime;
                        flightscheduleviewdata.FromCity=item.FromCity;
                        flightscheduleviewdata.FromAirport=item.FromAirport;
                        flightscheduleviewdata.ToAirport=item.ToAirport;
                        flightscheduleviewdata.ToCity=item.ToCity;
                        flightscheduleviewdata.ArrivalTime = item.ArrivalTime;
                        flightscheduleviewdatas.Add(flightscheduleviewdata);
                }

            }
            catch (Exception ex)
            {
                ServiceErrorException exception = new ServiceErrorException()
                {
                    ExceptionMessage = ex.Message,
                    InnerException = ex.InnerException != null ? ex.InnerException.Message : "",
                    StackTrace = ex.StackTrace,
                    Title = "Service Error..."
                };
                throw new FaultException<ServiceErrorException>(exception);
            }

            return flightscheduleviewdatas;
        }
    
        public ICollection<FlightScheduleViewData> GetFlightSchedulesByFlightDateFlightNumberCityFromCityTo(DateTime flightdate, string flightNumber, string cityFrom, string cityTo)
        {
              ICollection<FlightScheduleViewData> flightscheduleviewdatas = new List<FlightScheduleViewData>();
              FlightScheduleViewData flightscheduleviewdata=new FlightScheduleViewData();
            try
            {
                foreach (var item in _adminBF.GetFlightSchedulesByFlightDateFlightNumberCityFromCityTo(flightdate, flightNumber,cityFrom,cityTo))
                {
                        flightscheduleviewdata.FlightNo=item.FlightNumber;
                        flightscheduleviewdata.DepartureTime=item.DepartureTime;
                        flightscheduleviewdata.FromCity=item.FromCity;
                        flightscheduleviewdata.FromAirport=item.FromAirport;
                        flightscheduleviewdata.ToAirport=item.ToAirport;
                        flightscheduleviewdata.ToCity=item.ToCity;
                        flightscheduleviewdata.ArrivalTime = item.ArrivalTime;
                        flightscheduleviewdatas.Add(flightscheduleviewdata);
                }

            }
            catch (Exception ex)
            {
                ServiceErrorException exception = new ServiceErrorException()
                {
                    ExceptionMessage = ex.Message,
                    InnerException = ex.InnerException != null ? ex.InnerException.Message : "",
                    StackTrace = ex.StackTrace,
                    Title = "Service Error..."
                };
                throw new FaultException<ServiceErrorException>(exception);
            }

            return flightscheduleviewdatas;
        }
    }
        
}
